package Frame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Set;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

import Function.FCBlockLibrary;
import VWorldGameStudio.FCBlock;

/**
 * 编辑器主界面材质选择栏。Jpanel
 */
public class PngSelectJpanel extends JPanel {

	private Vector<String> blockname = new Vector<String>();
	private Image[] blockthum;// 缩略图
	private int selectnum = -1;
	private MapEditJpanel mapj;
	private int firsthangnum = 0;// 首行显示为第几行,一行4个

	public PngSelectJpanel(int x, int y, int width, int height) {
		setBounds(x, y, width, height);
		setBorder(BorderFactory.createLineBorder(Color.black));
		addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				selectnum = (int) (e.getX() / 50) + (int) (e.getY() / 60) * 4 + firsthangnum * 4;
				if (selectnum < blockthum.length)
					mapj.setBlockThum(blockthum[selectnum], blockname.get(selectnum));
				else
					selectnum = -1;
				repaint();
			}
		});
		addMouseWheelListener(new MouseWheelListener() {
			@Override
			public void mouseWheelMoved(MouseWheelEvent e) {
				if (e.getWheelRotation() == 1) {
					if (firsthangnum * 4 < blockthum.length - 5)
						firsthangnum++;
				} else {
					if (firsthangnum > 0)
						firsthangnum--;
				}
				repaint();
			}
		});
	}

	@Override
	protected void paintComponent(Graphics g) {
		// 绘制背景
		g.setColor(Color.white);
		g.fillRect(0, 0, getWidth(), getHeight());
		// 绘制缩略图
		g.setColor(Color.black);
		g.setFont(new Font("宋体", 0, 12));
		if (blockthum != null)
			for (int i = firsthangnum * 4; i < blockthum.length; i++) {
				g.drawImage(blockthum[i], 1 + 50 * ((i - firsthangnum * 4) % 4),
						1 + 60 * (int) ((i - firsthangnum * 4) / 4), this);
				g.drawString(blockname.get(i), 3 + 50 * ((i - firsthangnum * 4) % 4),
						61 + 60 * (int) ((i - firsthangnum * 4) / 4));
			}
		// 绘制选中
		g.setColor(Color.red);
		if (selectnum > -1)
			g.drawRect(1 + 50 * ((selectnum - firsthangnum * 4) % 4),
					1 + 60 * (int) ((selectnum - firsthangnum * 4) / 4), 48, 60);
	}

	// 加载方块
	public void loadBlockPngAndName(FCBlockLibrary bll) {
		blockthum = new Image[bll.getBlockAllNum()];
		blockname.clear();
		Set<String> qwe=bll.getBlockIDList();
		int i=0;
		for (String ID:qwe) {
			FCBlock bl = bll.getBlockOfID(ID);
			blockname.add(bl.getBlockID());
			createThum(bl.getBlockPngUrl(), bl.getBlockModel(), i);
			i++;
		}
		repaint();
	}

	// 创建缩略图
	private void createThum(String[] pngurl, int[][] model, int num) {
		BufferedImage newpng = new BufferedImage(model[0].length * 32, model.length * 32, BufferedImage.TYPE_4BYTE_ABGR);
		for (int i = 0; i < model.length; i++)
			for (int j = 0; j < model[0].length; j++) {
				if (model[i][j] > -1) {
					try {
						BufferedImage bp = ImageIO.read(new File(pngurl[model[i][j]]));
						for (int ii = 0; ii < 32; ii++)
							for (int jj = 0; jj < 32; jj++) {
									newpng.setRGB(jj + j * 32, ii + i * 32, bp.getRGB(jj, ii));
							}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		blockthum[num] = newpng.getScaledInstance(48, 48, Image.SCALE_DEFAULT);
	}

	public void setMapEditJpanel(MapEditJpanel mapj) {
		this.mapj = mapj;
	}
}
